from .listener import *

class QToolsPanelFuncs(QToolsPanelListener):

    def __init__(self, parent=None):
        super().__init__(parent=parent)

    def onBtnUIDesigner(self, evt):
        '''打开QT的ui设计器'''
        if wx.Shell("pyuic5 --version"):
            self.pid_pyside6_designer = wx.Execute("pyside6-designer", wx.EXEC_ASYNC)
        else:
            RichMsgDialog.showOkMsgDialog(self, '无法打开设计器，请确认QT环境是否已正确安装！', '失败')

    def onBtnChoiceUIandPath(self, evt):
        '''选择按钮/生成路径/确认'''
         # 校验QT命令是否存在
        if wx.Shell("pyuic5 --version"):
            if not self.isChoiceUI:
                uis = RichMsgDialog.showSelectFilesDialog(self, '.ui文件 (*.ui)|*.ui', '选择要转换的.ui文件')
                if len(uis) >= 1:
                    self.ui_py_btn_info += f'【{uis[0]}】'
                    self.order_data.append(uis[0])
                    self.btnChoiceUIandPath.SetLabel(BTN_CHOICE_UI_PY_PATH)
                    self.isChoiceUI = True
                else:
                    ...
            else:
                if not self.isChoiceGenPath:
                    path = RichMsgDialog.showSelectFoldersDialog(self, '选择.py生成路径')
                    if path is not None:
                        self.ui_py_btn_info += f'【{path}】'
                        self.order_data.append(path)
                        self.btnChoiceUIandPath.SetLabel(f'【{BTN_CHOICE_UI_PY_SURE}】{self.ui_py_btn_info}')
                        self.isChoiceGenPath = True
                    else:
                        ...
                else:
                    '''
                        执行命令操作
                    '''
                    py_file_name = os.path.basename(self.order_data[0]).split('.')[0].strip() + '.py'
                    py_path = os.path.join(self.order_data[1], py_file_name)
                    exec_order = f'pyuic5 -o {py_path} {self.order_data[0]}'
                    if wx.Shell(exec_order):
                        RichMsgDialog.showOkMsgDialog(self, '转换成功！', '成功')
                        '''
                            成功之后，自动将内容粘贴到剪切板
                        '''
                        if wx.TheClipboard.Open():
                            wx.TheClipboard.SetData(wx.TextDataObject(read_file(py_path)))
                            wx.TheClipboard.Close()
                            RichMsgDialog.showOkMsgDialog(self, '内容已成功复制到剪切板，可直接 Ctrl+V 粘贴使用。', '提示')
                    else:
                        RichMsgDialog.showOkMsgDialog(self, '转换失败！', '失败')

                    '''
                        最终状态：按钮状态初始化【不管成功与否均会执行】
                    '''
                    self.order_data.clear()
                    self.ui_py_btn_info = ''
                    self.isChoiceUI = False
                    self.isChoiceGenPath = False
                    self.btnChoiceUIandPath.SetLabel(BTN_CHOICE_UI_PY)
        else:
            RichMsgDialog.showOkMsgDialog(self, '请先安装QT for Python。', '错误')
